<?php
namespace app\home\controller;

use dingtalk\Auth;
use think\Controller;
use think\Cookie;
use think\Request;

class BaseController extends Controller
{
    private $accessToken;
    private $jsTicket;
    private $dingtalkConfig;
    private $dduser;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->accessToken=Auth::getAccessToken();
        $this->jsTicket=Auth::getTicket($this->accessToken);
        $this->dingtalkConfig = Auth::getConfig();
        if(Cookie::has('user')){
            $this->dduser = Cookie::get('user');
        }else{
            $this->redirect('Public/auth');
        }
        $this->assign('dduser',json_encode($this->dduser,true));
        $this->assign('auth_config',$this->dingtalkConfig);
    }

    /**
     * 获取用户信息
     * @return mixed
     */
    public function getDduser(){
        return $this->dduser;
    }

    /**
     * 获取accessToken
     * @return mixed
     */
    public function getAccessToken(){
        return $this->accessToken;
    }

    /**
     * 获取JsTicket
     * @return mixed
     */
    public function getJsTicket(){
        return $this->jsTicket;
    }

    /**
     * 获取钉钉配置参数
     * @return string
     */
    public function getddConfig(){
        return $this->dingtalkConfig;
    }

    /**
     * json返回数据
     * @param int $status 大于0为错误,1数据验证错误，2数据库运行错误
     * @param string|array $msg 错误信息
     * @param array|object $data 返回的信息
     * @return \think\response\Json|array
     */
    public function callBack($status, $msg, $data=[],$isJson=1)
    {
        if ($isJson){
            return json(['status'=>$status,'msg'=>$msg,'data'=>$data]);
        }else{
            return ['status'=>$status,'msg'=>$msg,'data'=>$data];
        }
    }

}
